#include "stdafx.h"
#include "SharedHandleTestSuite.h"

namespace CO { namespace SpiFoundation { namespace RoomFW {

SharedHandleTestSuite::SharedHandleTestSuite(void)
{
	TEST_ADD(SharedHandleTestSuite::all)
}

void SharedHandleTestSuite::all(void)
{
	// Use an event that's always signaled for testing.
	HANDLE raw = ::CreateEvent(0, true, true, 0);
	TEST_ASSERT(0 != raw);

	// Scope the handle to test dtors.
	{
		CO::SpiFoundation::RoomFW::SharedHandle handle(raw, CO::SpiFoundation::RoomFW::SharedHandleDeleter());
		TEST_ASSERT(0 != handle);
		DWORD firstWait = ::WaitForSingleObject(handle.get(), 0);
		TEST_ASSERT(WAIT_OBJECT_0 == firstWait);

		// Test sharing.
		{
			CO::SpiFoundation::RoomFW::SharedHandle handle2 = handle;
			TEST_ASSERT(0 != handle2);
			DWORD secondWait = ::WaitForSingleObject(handle2.get(), 0);
			TEST_ASSERT(WAIT_OBJECT_0 == secondWait);
		}
		// Test handle still valid.
		DWORD firstWait2 = ::WaitForSingleObject(handle.get(), 0);
		TEST_ASSERT(WAIT_OBJECT_0 == firstWait2);
	}
	// Test that handle was closed.
	DWORD finalWait = ::WaitForSingleObject(raw, 0);
	DWORD error = ::GetLastError();
	TEST_ASSERT(WAIT_FAILED == finalWait);
	TEST_ASSERT(ERROR_INVALID_HANDLE == error);
}

}}}
